//
//  SEGActivityCollectBrowseImageViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/4.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityCollectBrowseImageViewController.h"

@interface SEGActivityCollectBrowseImageViewController ()<UIScrollViewDelegate>
/**
 图片展示视图
 */
@property (nonatomic, strong)UIScrollView *scrollView;
@end

@implementation SEGActivityCollectBrowseImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showCustomNavigationRightButtonWithTitle:@"删除" image:nil hightlightImage:nil color:kColor_T];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight() - kNavigationBarAndStatusBarHeight)];
    [self.view addSubview:self.scrollView];
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.pagingEnabled = YES;
    
    [self.imageArray enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:obj];
        imageView.frame = CGRectMake(CGRectGetWidth(self.scrollView.frame) * idx, 0, CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
        imageView.tag = 100 + idx;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self.scrollView addSubview:imageView];
    }];
    self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.scrollView.frame) * self.currentIndex, 0);
    
    [self updateView];
}

//右侧按钮
- (void)onNavigationRightButtonClicked:(UIButton*)sender
{
    [self.imageArray removeObjectAtIndex:self.currentIndex];
    if (self.imageArray.count == 0) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (![obj isKindOfClass:[UIImageView class]]) {
            return;
        }
        if (obj.tag == self.currentIndex + 100) {
            [obj removeFromSuperview];
        }else if (obj.tag > self.currentIndex + 100){//之后的图片往前移动
            obj.frame = CGRectMake(CGRectGetWidth(self.scrollView.frame) * (idx - 1), 0, CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
        }
    }];
    //删除数组中的最后一个元素时
    self.currentIndex = self.imageArray.count <= self.currentIndex ? self.currentIndex - 1 : self.currentIndex;
    [self updateView];
}

- (void)updateView
{
    self.title = [NSString stringWithFormat:@"%lu/%lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.imageArray.count];
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame) * self.imageArray.count, CGRectGetHeight(self.view.frame));

//    //先清空所有图片
//    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        if (obj.tag >= 100 && [obj isKindOfClass:[UIImageView class]]) {
//            [obj removeFromSuperview];
//        }
//    }];
    

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //计算当前位于哪一个页 计算方法：当前偏移的X/滚动视图的宽度
    self.currentIndex = self.scrollView.contentOffset.x / self.scrollView.frame.size.width;
    [self updateView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
